from django.db import models

# Create your models here.
from dvadmin.system.models import Users
from dvadmin.utils.models import CoreModel, table_prefix, SoftDeleteModel

STATUS = ((0, "禁用"), (1, "启用"),)
ACCOUNT_TYPE = ((1, "认证服务号"), (2, "认证媒体"), (3, "政府订阅号"), (4, "个人"),)
SUBSCRIBE = ((0, "未订阅"), (1, "订阅"),)
IS_BLACK = ((0, "拉黑"), (1, "未拉黑"),)


class OfficialAccountManagement(CoreModel,SoftDeleteModel):
    """公众号管理"""
    head_portrait = models.CharField(max_length=255, blank=True, null=True, verbose_name="头像", help_text="头像")
    qr_code = models.CharField(blank=True, null=True, max_length=255, verbose_name="二维码", help_text="二维码")
    name = models.CharField(max_length=64, verbose_name="公众号名字", help_text="公众号名字")
    account_number = models.CharField(max_length=64, verbose_name="账号", help_text="账号")
    original_id = models.CharField(max_length=64, verbose_name="原始id", help_text="原始id")
    status = models.IntegerField(choices=STATUS, default=1, verbose_name="状态", help_text="状态")
    account_type = models.IntegerField(choices=ACCOUNT_TYPE, default=1, verbose_name="账号类型", help_text="账号类型")
    appid = models.CharField(max_length=255, blank=True, null=True, verbose_name="appid", help_text="appid")
    app_secret = models.CharField(max_length=255, blank=True, null=True, verbose_name="app_secret",
                                  help_text="app_secret")
    url = models.CharField(max_length=255, blank=True, null=True, verbose_name="url", help_text="url")  # 服务器地址
    token = models.CharField(max_length=255, blank=True, null=True, verbose_name="token", help_text="token")  # 令牌
    encoding_aes_key = models.CharField(max_length=255, blank=True, null=True, verbose_name="encoding_aes_key",
                                        help_text="encoding_aes_key")  # 消息加解密秘钥

    class Meta:
        db_table = table_prefix + "official_account_management"
        verbose_name = "公众号管理"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)


class FanTags(CoreModel,SoftDeleteModel):
    official_account_management = models.ForeignKey(
        to="OfficialAccountManagement", db_constraint=False, blank=True, null=True,
        related_name="fan_tags_official_account_management",
        on_delete=models.PROTECT, verbose_name="关联公众号", help_text="关联公众号", )
    name = models.CharField(max_length=20, blank=True, null=True, verbose_name="粉丝标签", help_text="粉丝标签")
    tagsid = models.CharField(max_length=20, blank=True, null=True, verbose_name="微信标签id", help_text="微信标签id")

    class Meta:
        db_table = table_prefix + "fan_tags"
        verbose_name = "粉丝标签管理"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)


class FanGroups(CoreModel,SoftDeleteModel):
    official_account_management = models.ForeignKey(
        to="OfficialAccountManagement", db_constraint=False, blank=True, null=True,
        related_name="fan_groups_official_account_management",
        on_delete=models.PROTECT, verbose_name="关联公众号", help_text="关联公众号", )
    name = models.CharField(max_length=20, blank=True, null=True, verbose_name="组名", help_text="组名")
    groupid = models.CharField(max_length=20, blank=True, null=True, verbose_name="微信组id", help_text="微信组id")

    class Meta:
        db_table = table_prefix + "fan_groups"
        verbose_name = "粉丝分组管理"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)


class FanManagement(CoreModel,SoftDeleteModel):
    """粉丝管理"""
    official_account_management = models.ForeignKey(
        to="OfficialAccountManagement", db_constraint=False, blank=True, null=True,
        related_name="fan_management_official_account_management",
        on_delete=models.PROTECT, verbose_name="关联公众号", help_text="关联公众号", )
    name = models.CharField(max_length=20,  blank=True, null=True,verbose_name="粉丝昵称", help_text="粉丝昵称")
    head_portrait = models.CharField(max_length=255,  blank=True, null=True,verbose_name="头像", help_text="头像")
    open_id = models.CharField(max_length=255, verbose_name="openid", help_text="openid")
    union_id = models.CharField(max_length=255, blank=True, null=True, help_text="unionid")
    subscribe = models.IntegerField(choices=SUBSCRIBE, default=1, verbose_name="是否订阅", help_text="是否订阅")
    language = models.CharField(max_length=64, blank=True, null=True, verbose_name="语言", help_text="语言")
    subscribe_time = models.DateTimeField(null=True, blank=True, help_text="关注时间", verbose_name="关注时间")
    remark = models.CharField(blank=True, null=True, max_length=255, verbose_name="备注", help_text="备注")
    fan_groups = models.ForeignKey(
        to="FanGroups", db_constraint=False, blank=True, null=True, related_name="fan_groups",
        on_delete=models.PROTECT, verbose_name="关联粉丝组", help_text="关联粉丝组")
    fan_tags = models.ManyToManyField(to="FanTags", verbose_name="粉丝标签", db_constraint=False, help_text="粉丝标签")
    is_black = models.IntegerField(choices=IS_BLACK, default=1, verbose_name="是否拉黑", help_text="是否拉黑")
    subscribe_scene = models.CharField(max_length=64, verbose_name="关注渠道", help_text="关注渠道")

    class Meta:
        db_table = table_prefix + "fan_management"
        verbose_name = "粉丝管理"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)
        unique_together = [["open_id", "official_account_management"]]
